package com.bstek.urule.console.servlet.flow;

import com.bstek.urule.RuleException;
import com.bstek.urule.Utils;
import com.bstek.urule.console.servlet.base.BaseController;
import com.bstek.urule.console.repository.RepositoryService;
import com.bstek.urule.console.repository.model.ResourcePackage;
import com.bstek.urule.model.flow.FlowDefinition;
import com.bstek.urule.parse.deserializer.FlowDeserializer;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zhouchaoliang
 * @since 2021/12/9
 */
@Controller
public class RuleFlowDesignerController extends BaseController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private FlowDeserializer flowDeserializer;

    @GetMapping("/urule/ruleflowdesigner")
    public ModelAndView flow(HttpServletRequest req, HttpServletResponse resp) {
        ModelAndView mv = new ModelAndView("/rule-flow-designer");
        mv.addObject("contextPath", req.getContextPath());
        String file = req.getParameter("file");
        String project = buildProjectNameFromFile(file);
        if (project != null) {
            mv.addObject("project", project);
        }
        return mv;
    }

    @ResponseBody
    @GetMapping("/urule/ruleflowdesigner/loadFlowDefinition")
    public FlowDefinitionWrapper loadFlowDefinition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream inputStream;
        String file=req.getParameter("file");
        String version=req.getParameter("version");
        file= Utils.decodeURL(file);
        try{
            if(StringUtils.isEmpty(version)){
                inputStream=repositoryService.readFile(file,null);
            }else{
                inputStream=repositoryService.readFile(file,version);
            }
            Element root=parseXml(inputStream);
            FlowDefinition fd = flowDeserializer.deserialize(root);
            inputStream.close();
            return new FlowDefinitionWrapper(fd);
        }catch(Exception ex){
            throw new RuleException(ex);
        }
    }

    @PostMapping("/urule/ruleflowdesigner/loadPackages")
    public List<ResourcePackage> loadPackages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String project=req.getParameter("project");
        try{
            return repositoryService.loadProjectResourcePackages(project);
        }catch(Exception ex){
            throw new RuleException(ex);
        }
    }

    protected Element parseXml(InputStream stream){
        SAXReader reader=new SAXReader();
        Document document;
        try {
            document = reader.read(stream);
            Element root=document.getRootElement();
            return root;
        } catch (DocumentException e) {
            throw new RuleException(e);
        }
    }
}
